import { login,userInfo,headPortraits } from "@/api/user.js";
import { getToken,setToken,removeToken} from "@/utils/auth"
import {resetRouter} from "@/router"

export default {
  namespaced: true,
  state: {
    token: getToken()||'',
    user:{}
  },
  mutations: {
    // 获取token值
    updateLogin(state,token){
      state.token=token
      setToken(token)
    },
    // 删除token值
    removeToken(state,token){
      state.token=token
      removeToken()
    },
    // 获取用户信息
    userInf(state,user){
      state.user = user
    },
    // 清空用户信息
    removeUserInf(state){
      state.user = {}
    },
    
  },
  actions: {
    // 获取token值
   async userLogin(context,loginForm){
      const  res = await login(loginForm)
            // console.log(res.data);
            // console.log(1);
    context.commit('updateLogin',res.data)
    },
       // 获取用户信息
       async userInfos(context){
        try {
          const  res = await userInfo()
          console.log(res);
          
          const  info = await headPortraits(res.data.userId)
          // console.log(info);
          context.commit('userInf',{...res.data,...info.data})
          return res.data.roles.menus
        } catch (error) {
          console.log(error);
        }
      },
      // 清除用户信息  token值
    logout(context){
      context.commit('removeToken')
      context.commit('removeUserInf')
      resetRouter()
    }
  }
}

